package vn.edu.fpt.xml.back.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Override JPA properties using properties file.
 *
 * Created by hiephn on 2014/04/27.
 */
public class XmlNewsCloudBackendLocalEntityManagerFactoryBean extends LocalEntityManagerFactoryBean {

    protected final static Logger LOGGER = LoggerFactory.getLogger(XmlNewsCloudBackendLocalEntityManagerFactoryBean.class);

    @Resource(name = "xnConfiguration")
    protected XnConfiguration xnConfiguration;

    @Override
    public Map<String, Object> getJpaPropertyMap() {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Initialize custom JPA properties");
        }

        Map<String, Object> jpaPropertyMap = super.getJpaPropertyMap();
        jpaPropertyMap.put("hibernate.connection.driver_class", xnConfiguration.getHibernateConnectionDriverClass());
        jpaPropertyMap.put("hibernate.connection.url", xnConfiguration.getHibernateConnectionUrl());
        jpaPropertyMap.put("hibernate.connection.username", xnConfiguration.getHibernateConnectionUsername());
        jpaPropertyMap.put("hibernate.connection.password", xnConfiguration.getHibernateConnectionPassword());
        jpaPropertyMap.put("hibernate.dialect", xnConfiguration.getHibernateDialect());
        jpaPropertyMap.put("hibernate.connection.pool_size", xnConfiguration.getHibernateConnectionPoolSize());
        jpaPropertyMap.put("hibernate.connection.autocommit", xnConfiguration.isHibernateConnectionAutocommit());
        jpaPropertyMap.put("hibernate.transaction.flush_before_completion", xnConfiguration.isHibernateTransactionFlushBeforeCompletion());
        jpaPropertyMap.put("hibernate.transaction.auto_close_session", xnConfiguration.isHibernateTransactionAutoCloseSession());

        return jpaPropertyMap;
    }
}
